<template>
  <!--  <page-header-wrapper>-->
  <a-card :bordered="false">
    <a-form layout="inline">
      <a-row :gutter="48">
        <a-col :md="5" :sm="24">
          <a-form-item label="设备型号">
            <a-input v-model="queryParam.id" placeholder="请输入设备型号"/>
          </a-form-item>
        </a-col>
        <a-col :md="5" :sm="24">
          <a-form-item label="设备名称">
            <a-input v-model="queryParam.id" placeholder="请输入设备名称"/>
          </a-form-item>
        </a-col>
        <a-col :md="5" :sm="24">
          <a-form-item label="规格">
            <a-input v-model="queryParam.id" placeholder="请输入规格"/>
          </a-form-item>
        </a-col>
        <a-col :md="5" :sm="24">
          <a-form-item label="生产厂商">
            <a-input v-model="queryParam.id" placeholder="请输入生产厂商"/>
          </a-form-item>
        </a-col>
        <a-col :md="!advanced && 4 || 5" :sm="24">
          <span class="table-page-search-submitButtons" :style="advanced && { float: 'left', overflow: 'hidden' } || {} ">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
          </span>
        </a-col>
      </a-row>
    </a-form>
    <div style="width: 100%;height: 30px;" />
    <s-table
      ref="table"
      bordered
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :rowSelection="rowSelection"
      showPagination="auto"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="description" slot-scope="text">
        <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleSub1(record.id)">
            <a style="color: #ED3F14">删除</a>
          </a-popconfirm>
        </template>
      </span>
    </s-table>
    <!--  编辑 modal  -->
    <a-modal v-model="visible" :width="500" title="修改机器设备" @ok="handleOk">
      <a-form layout="inline" :form="form">
        <a-row style="width: 100%;margin-bottom: 180px;">
          <a-form-item label="试妆参数" hasFeedback>
            <a-select style="width: 240px" v-decorator="['type', {}]" placeholder="请选择服务号类型">
              <a-select-option value="0">没有微信服务号</a-select-option>
              <a-select-option value="1">JM跨境购</a-select-option>
              <a-select-option value="2">唯品会合肥城市奥莱服务号</a-select-option>
              <a-select-option value="3">挑剔小姐MISS PICKY</a-select-option>
              <a-select-option value="4">美丽旅程</a-select-option>
              <a-select-option value="5">海蜇优品</a-select-option>
            </a-select>
          </a-form-item>
        </a-row>
      </a-form>
    </a-modal>
  </a-card>
  <!--  </page-header-wrapper>-->
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getRoleList, getServiceList } from '@/api/manage'

const columns = [
  {
    title: '#',
    align: 'center',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '设备型号',
    align: 'center',
    dataIndex: 'no'
  },
  {
    title: '设备名称',
    align: 'center',
    dataIndex: 'description',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '规格',
    align: 'center',
    dataIndex: 'callNo',
    sorter: true,
    needTotal: true,
    customRender: (text) => text + ' 次'
  },
  {
    title: '生产厂商',
    align: 'center',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '入库人员',
    align: 'center',
    dataIndex: 'description',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '库存数量',
    align: 'center',
    dataIndex: 'callNo',
    sorter: true,
    needTotal: true,
    customRender: (text) => text + ' 次'
  },
  {
    title: '入库数量',
    align: 'center',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '入库时间',
    align: 'center',
    dataIndex: 'updatedAt',
    sorter: true
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]
const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'DeviceAccount',
  components: {
    STable,
    Ellipsis
  },
  data () {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return getServiceList(requestParameters)
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    getRoleList({ t: new Date() })
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk () {
      this.visible = false
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
